'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-11-01 15:12:15
'''
#
# @lc app=leetcode.cn id=530 lang=python3
#
# [530] 二叉搜索树的最小绝对差
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def getMinimumDifference(self, root: TreeNode) -> int:
        from sys import maxsize
        if not root:
            return 0
        ans = [maxsize, -maxsize]
        def recurse(root):
            if not root:
                return
            recurse(root.left)
            ans[0] = min(ans[0], root.val-ans[1])
            ans[1] = root.val
            recurse(root.right)
        recurse(root)
        return ans[0]
# @lc code=end

